package edu.columbia.cloudbox.ui;

import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import edu.columbia.cloudbox.CloudBoxActivity;
import edu.columbia.cloudbox.R;
import edu.columbia.cloudbox.UserStatusService;
import edu.columbia.cloudbox.policy.Policy;
import edu.columbia.cloudbox.policy.UserPreference;

public class ConfigActivity extends Activity {

	private static final String className = ConfigActivity.class
			.getSimpleName();

	private List<String> userExts = new Vector<String>();
	private int userPolicy = 0;
	private long userInterval = 0L;

	ListView config_list_view;
	RadioButton config_manual;
	RadioButton config_auto;
	RadioGroup config_policy;
	Spinner config_spinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);

		config_list_view = (ListView) findViewById(R.id.config_list_view);
		config_manual = (RadioButton) findViewById(R.id.config_manual);
		config_auto = (RadioButton) findViewById(R.id.config_auto);
		config_policy = (RadioGroup) findViewById(R.id.config_policy);
		config_spinner = (Spinner) findViewById(R.id.config_spinner);

		UserStatusService.user_preference.loadPreference();
		userExts.addAll(UserPreference.userExts);
		userPolicy = UserPreference.userPolicy;
		userInterval = UserPreference.userInterval;

		Log.i(className, "UserPreference.isManual " + UserPreference.isManual);
		if (UserPreference.isManual) {
			config_manual.setChecked(true);
			Log.i(className, "displayManual(config_list_view); ");
			displayManual(config_list_view);
		} else {
			config_auto.setChecked(true);
			displayAuto(config_list_view);
		}
		setOKButton();
		setSpinner();

		config_policy
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if (config_manual.isChecked()) {
							UserPreference.isManual = true;
							displayManual(config_list_view);

						} else if (config_auto.isChecked()) {
							UserPreference.isManual = false;
							displayAuto(config_list_view);

						}

					}

				});
	}

	@Override
	protected void onResume() {
		super.onResume();

		UserStatusService.user_preference.loadPreference();
		userExts.addAll(UserPreference.userExts);
		userPolicy = UserPreference.userPolicy;
		userInterval = UserPreference.userInterval;

		Log.i(className, "UserPreference.isManual " + UserPreference.isManual);
		if (UserPreference.isManual) {
			config_manual.setChecked(true);
			Log.i(className, "displayManual(config_list_view); ");
			displayManual(config_list_view);
		} else {
			config_auto.setChecked(true);
			displayAuto(config_list_view);
		}

		setOKButton();
		setSpinner();

		Log.i(className, "UserPreference.isManual " + UserPreference.isManual);
		if (UserPreference.isManual) {
			config_manual.setChecked(true);
			Log.i(className, "displayManual(config_list_view); ");
			displayManual(config_list_view);
		} else {
			config_auto.setChecked(true);
			displayAuto(config_list_view);
		}
		setOKButton();
		setSpinner();

		config_policy
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if (config_manual.isChecked()) {
							UserPreference.isManual = true;
							displayManual(config_list_view);

						} else if (config_auto.isChecked()) {
							UserPreference.isManual = false;
							displayAuto(config_list_view);

						}

					}

				});

	}

	private void displayAuto(ListView listView) {
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		config_list_view.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				userPolicy = arg2;
				UserPreference.userPolicy = userPolicy;
				Log.i(className, "selected: " + arg2);
			}
		});

		ArrayAdapter<Policy.AutoPolicy> adapter = new ArrayAdapter<Policy.AutoPolicy>(
				ConfigActivity.this,
				android.R.layout.simple_list_item_multiple_choice,
				Policy.AutoPolicy.values());
		listView.setAdapter(adapter);
		listView.setItemChecked(UserPreference.userPolicy, true);
	}

	private void displayManual(ListView listView) {
		final String methodName = "displayManual(ListView listView)";
		Log.i(className, methodName + ": Entering...");

		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		config_list_view.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				SparseBooleanArray checked = config_list_view
						.getCheckedItemPositions();
				Log.i(className, "checked==null " + (checked == null));
				userExts.clear();
				for (int i = 0; i < config_list_view.getAdapter().getCount(); i++) {
					if (checked.valueAt(i)) {
						userExts.add(config_list_view.getAdapter().getItem(i)
								.toString());
					}
				}
				Log.i(className, "selected: " + userExts);
			}

		});

		List<String> extList = UserStatusService.S3IO_INSTANCE
				.getExts(UserStatusService.USER_NAME);
		String[] array_spinner = new String[extList.size()];
		extList.toArray(array_spinner);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				ConfigActivity.this,
				android.R.layout.simple_list_item_multiple_choice,
				array_spinner);
		listView.setAdapter(adapter);

		Log.i(className, methodName + "exts: " + extList);

		for (int i = 0; i < listView.getAdapter().getCount(); i++) {
			String ext = listView.getAdapter().getItem(i).toString();
			if (UserPreference.userExts.contains(ext)) {
				Log.i(className, methodName + "check: " + i);
				listView.setItemChecked(i, true);
			}
		}
	}

	private void setOKButton() {
		CloudBoxActivity.backButton.setVisibility(View.VISIBLE);
		CloudBoxActivity.backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				UserPreference.isManual = config_manual.isChecked();
				UserPreference.userExts.clear();
				UserPreference.userExts.addAll(userExts);
				UserPreference.userPolicy = userPolicy;
				Log.i(className, "userInterval=" + userInterval);
				UserPreference.userInterval = userInterval;
				UserStatusService.user_preference
						.savePreference(ConfigActivity.this);

				// Log.i(className, "UserPreference.isManual "
				// + UserPreference.isManual);
				// if (UserPreference.isManual) {
				// config_manual.setChecked(true);
				// Log.i(className, "displayManual(config_list_view); ");
				// displayManual(config_list_view);
				// } else {
				// config_auto.setChecked(true);
				// displayAuto(config_list_view);
				// }
			}

		});
	}

	private void setSpinner() {
		final Long[] array = { 10000L, 20000L, 30000L };
		ArrayAdapter<Long> adapter = new ArrayAdapter<Long>(
				ConfigActivity.this, android.R.layout.simple_spinner_item,
				array);
		config_spinner.setAdapter(adapter);

		config_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Log.i(className, "selected: " + array[arg2]);
				userInterval = array[arg2];
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
	}
}
